"""
Author: Morphlng
Date: 2024-01-23 13:26:26
LastEditTime: 2024-04-17 13:12:03
LastEditors: Morphlng
Description: Factory for creating simulators.
FilePath: /DrivingGym/src/driving_gym/simulation/adapter_factory.py
"""

from __future__ import annotations

import os
from driving_gym.simulation.adapter_interface import AdapterInterface
from driving_gym.misc.util import dynamic_import


class AdapterFactory:
    """Factory for creating simulators."""

    @staticmethod
    def create(config: dict, adapter_type: str = None) -> AdapterInterface:
        """Creates a simulator based on config.

        Args:
            config (dict): The configuration for the adapter.
            adapter_type (str): The type of simulator adapter to create. Specify if not in config.

        Returns:
            The adapter.
        """
        adapter_type = adapter_type or config["type"].lower()
        adapter_type = adapter_type.replace("_adapter", "")

        adapter = dynamic_import(
            adapter_type,
            os.path.join(os.path.dirname(__file__), f"adapter/{adapter_type}"),
            args=(config,),
            suffix="_adapter",
        )

        if adapter is None:
            raise ValueError(f"{adapter_type} adpater not found.")
        return adapter
